// 文件名: MyThread.java（修正版）
package Thread;

import java.util.Objects;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    static Lock lock = new ReentrantLock();
    static int anInt = 100;
    Random random =  new Random();
    @Override
    public void run() {
        while (true) {
                lock.lock();
            try {
                if (anInt==0) {
                    break;
                } else {
                    if (anInt%2!=0)
                    System.out.println(getName() + "----" + anInt);
                        Thread.sleep(10);
                    anInt--;
                }// 确保在退出循环时释放锁。
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}